package test;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Vector;

/**
 * Created by Kong1997 on 2017/7/10.
 */
public class Enemy {

    BufferedImage imgs[] = new BufferedImage[5];
    BufferedImage imgs1[] = new BufferedImage[6];
    BufferedImage imgs2[] = new BufferedImage[9];
    public int x = (int)(20+Math.random()*400);  //初始化敌机位置
    public int y = -(int)(10+Math.random()*200);
    public int status = 0;  //正常0  被击中1
    public int count = 1;  //爆炸效果计数
    public int flag = 0;  //是否销毁
    public int enemyCate = 0; //敌机种类 0小  1中  2大
    public Enemy(int Cate){
        enemyCate = Cate;
        switch (Cate){
            case 0:
                for (int i = 0; i <imgs.length ; i++) {
                    try {
                        imgs[i] = ImageIO.read(getClass().getResource("/npc0_"+i+".png"));
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }

                break;
            case 1:
                for (int i = 0; i < imgs1.length; i++) {
                    try {
                        imgs1[i] = ImageIO.read(getClass().getResource("/npc1_"+i+".png"));
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }

                break;
            case 2:
                for (int i = 0; i < imgs2.length; i++) {
                    try {
                        imgs2[i] = ImageIO.read(getClass().getResource("/npc2_"+i+".png"));
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }

                break;
        }

    }

    public void paint(Graphics g){
        if(status == 0){
            switch (enemyCate){  //正常状态飞机
                case 0:
                    g.drawImage(imgs[0],x,y,null);
                    break;
                case 1:
                    g.drawImage(imgs1[0],x,y,null);
                    break;
                case 2:
                    g.drawImage(imgs2[0],x,y,null);
                    break;
            }
        }
        else {
            switch (enemyCate){
                case 0:
                    if(count<=(imgs.length-1)) {
                        g.drawImage(imgs[count], x, y, null); //敌机被击中爆炸效果
                        count++;
                    }
                    else {
                        count = 1;
                        g.drawImage(null, x, y, null);  //爆炸效果完成 销毁敌机
                        flag = 1; //飞机销毁标志
                    }
                    break;

                case 1:
                    if(count<=(imgs1.length-1)) {
                        g.drawImage(imgs1[count], x, y, null); //敌机被击中爆炸效果
                        count++;
                    }
                    else {
                        count = 1;
                        g.drawImage(null, x, y, null);  //爆炸效果完成 销毁敌机
                        flag = 1; //飞机销毁标志
                    }

                    break;

                case 2:
                    if(count<=(imgs2.length-1)) {
                        g.drawImage(imgs2[count], x, y, null); //敌机被击中爆炸效果
                        count++;
                    }
                    else {
                        count = 1;
                        g.drawImage(null, x, y, null);  //爆炸效果完成 销毁敌机
                        flag = 1; //飞机销毁标志
                    }

                    break;
            }
        }

    }

    public void move(Vector<Enemy> enemys){
        y+=4;
        if(flag == 1){
            enemys.remove(this);  //销毁该敌机
        }
    }


}
